<template>
	<view :style="viewColor">
		<view class="address-window popup-main bg-f" style="height:85%" :class="display==true?'on':''">
			<view style="margin-top: 10px;margin-left: 10px;" >
				
				<view class="display-flex display-flex-align-center display-flex-justify-center mb-20" style="font-weight: 600;font-size: 18px;text-align: center;">
					<view class="display-flex">
						<view class="">
							选择乘客
						</view>
						<view @click.stop="close" class="display-flex ml-auto " style="position: absolute; right: 0;">
							<view class="iconfont icon-ic_close-2 text-24  mr-10"></view>
						</view>
					</view>
				</view>
				<view  class="display-flex display-flex-justify-center">
					<view @click="addPa" class="" style="border-radius: 20px;height: 40px;line-height: 40px;width: 90%;color: #2979ff;margin: 15px;background-color: #e5f9ff;text-align: center;">
						<text class="iconfont icon-jia-yuankuang" style="margin-right: 5px;"></text> 
						<text >新增乘客</text>
					</view>
				</view>
				
				<scroll-view class="" scroll-y="true" style="height: 800rpx;">
					
					<view  class=" pa-item" v-for="(item, index) in passengerList" :key="index">
						<view class="row-center" style="margin: 5px 12px;">
							<text @click="editPassenger(item)"  class="iconfont icon-ic_pencil " style="font-size: 25px;color: var(--view-theme);margin-right: 15px;"></text>
							<text style="font-size: 14px;color: #000000;font-weight: 600;width: 70px;">{{item.name}}</text>
							<text style="color: #909090;font-size: 12px;margin: 0 5px;margin-right: 25px;">{{item.id_card}}</text>
							<view class="idcard-tip" >
								{{item.type==1?'成人':'儿童'}}
							</view>
				
							<view @click="selectPassenger(index,item.check)"  v-if="item.check" class="iconfont icon-a-ic_CompleteSelect" style="margin-left: 12px;font-size: 25px;color:var(--view-theme);">
								
							</view>
							<view @click="selectPassenger(index,item.check)"  v-else class="iconfont  icon-ic_unselect" style="margin-left: 12px;font-size: 25px;color: var(--view-theme);">
								
							</view>
							<!-- <view @click="deletePassenger(item.id,index)"   class="iconfont icon-ic_delete" style="margin-left: 12px;font-size: 25px;color: #ff0000;">
								
							</view> -->
						</view>
						<view v-if="index!=(passengerList.length-1)" class="separation-line" style="margin: 12px;">
							
						</view>
					</view>
			
				</scroll-view>
				<view class="display-flex display-flex-align-center display-flex-justify-center bg-white" style="position: absolute;bottom: 0;width: 100%;">
					<view @click="confirmSelect" class="q-confirm">
						<view>确认选择</view>
					</view>
				</view>
			</view>
			
		</view>
		<view class='mask' catchtouchmove="true" :hidden='display==false' @tap='close'></view>
		
	</view>
</template>

<script>

	
	import {deletePassenger} from '@/api/user.js';
	import { mapGetters } from "vuex";
	const CACHE_ADDRESS = {};	
	export default {
		props: {
			display: {
				type: Boolean,
				default: true
			},
			cityShow: {
				type: Number,
				default: 3
			},
			list: {
				type:Array ,
				default:[]
			},
			selectList: {
				type:Array ,
				default:[]
			},
			passengerNum: {
				type: Number,
				default: 0
			},
			skuIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				showAddPassenger: false,
				passengerList: [],
			};
		},
		components: {
		},
		computed:{
			...mapGetters(['viewColor']),
		
		},
		watch:{
		
			display(n){
				if(n){
					this.passengerList = JSON.parse(JSON.stringify(this.list))
					console.log('this.passengerList1',this.passengerList)
					console.log('this.selectList',this.selectList)
					var that = this;
					if(this.selectList.length){
						for (var i = 0; i <this.passengerList.length; i++ ) {
							// for(var j= 0;j < this.passengerList.length; j++){
							// 	console.log(1234,this.passengerList[j].id ==this.selectList[i].id)
							// 	if(this.passengerList[j].id ==this.selectList[i].id){
							// 		this.passengerList[j].check = true;
							// 	} else{
							// 		this.passengerList[j].check = false;
							// 	}
							// }
							const item =that.selectList.find(function(element) {
							  return that.passengerList[i].id == element.id;  
							});
							if(item != undefined){
								that.passengerList[i].check = true;
							} else {
								that.passengerList[i].check = false;
							}
						}
						console.log('this.passengerList2',that.passengerList)
					}else{
						that.passengerList.forEach(function(res,index) {
							that.passengerList[index].check = false
						})	
					}
					
				}
			},
			// list:{
			// 	handler(nVal,oVal){
			// 		if(nVal){
			// 			var that = this;
			// 			that.passengerList.forEach(function(res,index) {
			// 				that.passengerList[index].check = false
			// 			})	
			// 		}
			// 	},
			// 	immediate: true,
			// 	deep:true
			// }
			
		},
		mounted() {
			
			// if(this.list.length){
			// 	for(var i; i<this.list.length;i++){
			// 		this.list[i].check = false;
			// 		this.passengerList.push(this.list[i])
			// 	}
			// }
			this.passengerList = JSON.parse(JSON.stringify(this.list))
			console.log('passengerList',this.passengerList)
		},
		methods: {
			close: function() {
				this.$emit('changeClose');
			},
			addPa(){
				this.$emit('addPa');
			},
			editPassenger(item){
				this.$emit('editPassenger',item);
			},
			selectPassenger:function(index, isCheck){
				console.log(index, isCheck)
				
				if(this.passengerNum == 1){
					for(var i =0; i < this.passengerList.length; i++){
						if(index == i){
							 this.passengerList[i].check = true;
						}else{
							this.passengerList[i].check = false;
						}
					}
				} else {
					var passNum = 0;
					this.passengerList.forEach(function(res){
						if(res.check==true){
							passNum++
						}
					})
					if(passNum == this.passengerNum && isCheck == false){
						return this.$util.Tips({title: "您只需选择"+this.passengerNum+'位游客'})
					}
					this.passengerList[index].check = !this.passengerList[index].check;
				}
				console.log('this.passengerList', this.passengerList)
				// var passNum = 0;
				// this.passengerList.forEach(function(res){
				// 	if(res.check==true){
				// 		passNum++
				// 	}
				// })
				
				
				
			},
			confirmSelect(){
				var arr = [];
				for(var i =0; i < this.passengerList.length; i++){
					if( this.passengerList[i].check == true){
						 arr.push( this.passengerList[i])
					}
				}
				this.$emit('changePassenger',arr,this.skuIndex);
			},
			deletePassenger(id,index){
				var that = this
				uni.showModal({
				    title: '提示',
				    content: '确认删除游客？',
					confirmText: '确认删除',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
							deletePassenger({id: id}).then(res => {
								that.passengerList.splice(index,1)
								that.$emit('changePassenger',that.passengerList);
							}).catch(err => {
								 that.$util.Tips(err.message)
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			
		}
	}
</script>

<style scoped lang="scss">
	.q-confirm{
		border-radius: 20px;
		height: 40px;
		line-height: 40px;
		color: #FFFFFF;
		margin: 20px;
		background-color: var(--view-theme);
		text-align: center;
		width: 300px;
		// position: absolute;
		// bottom: 0;
	}
	.address-window .title {
		height: 123rpx;
		line-height: 123rpx;
	}
	.address-window .title .iconfont {
		position: absolute;
		right: 28rpx;
		top: 30rpx;
	}
	.address-count{
		.address-selected{
			padding: 0 30rpx;
			margin-top: 10rpx;
			position: relative;
			padding-bottom: 20rpx;
			border-bottom: 2rpx solid #f7f7f7;
		}
		.selected-list{
			font-size: 26rpx;
			color: #282828;
			line-height: 50rpx;
			padding-bottom: 10rpx;
			padding-left: 60rpx;
			position: relative;
			&.active{
				color: var(--view-theme);
			}
			&:before,&:after{
				content: '';
				display: block;
				position: absolute;			
			}
			&:before{
				width: 4rpx;
				height: 100%;
				background-color: var(--view-theme);
				top: 0;
				left: 10rpx;
			}
			&:after{
				width: 12rpx;
				height: 12rpx;
				background: var(--view-theme);
				border-radius: 100%;
				left: 6rpx;
				top: 50%;
				margin-top: -8rpx;
			}
			&:first-child,&:last-child{
				&:before{
					height: 50%;
				}
			}
			&:first-child{
				&:before{
					top: auto;
					bottom: 0;
				}
			}
			.iconfont{
				font-size: 28rpx;
				float: right;
				color: #dddddd;
			}
		}
		scroll-view{
			height: 550rpx;
		}
		.address-list{
			padding: 0 30rpx;
			margin-top: 20rpx;
			box-sizing: border-box;
			.list{
				.iconfont{
					float: right;
					color: #ddd;
					font-size: 28rpx;
				}
				.item-name{
					display: inline-block;
					line-height: 50rpx;
					margin-bottom: 20rpx;
					font-size: 26rpx;
				}
				&.active{
					color: var(--view-theme);
					.iconfont{
						color: var(--view-theme);
					}
				}
			}
		}
	}
	.pa-item{
		padding: 10px;
	}
	.pa-item:last-child {
	  padding-bottom: 120px;
	}
</style>
